iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 7
1
自我挑戰組

LINE BOT 新手村30日攻略系列 第 7

Day07 進階訊息傳送1 - Actions

  • 分享至 

  • xImage
  •  

在上一篇我們介紹過基本的訊息傳送方式後
今天要來介紹更蝦趴的訊息傳送方式了
之前在做口罩查詢系統的第二版進行美觀修正就是加入了進階的訊息傳送方式
但是在開始正式介紹訊息傳送方式前,我們要先介紹這些訊息特有的功能 Actions
透過 Actions ,可以讓LINE訊息有更多發展的空間

官方文檔
在 LINE 提供的 Actions 中,包含了

  • Postback Action
  • Message Action
  • URI Action
  • Datetime Picker Action
  • Camera Action
  • Camera Roll Action
  • Location Action

事不宜遲,現在馬上來介紹各種 Action

Postback Action

Postback Action 會傳送一個指定的字串,以 postback event 的形式回傳
例如在我們點擊按鈕後,會自動傳送一串字給 LINE Platform
接下來你可以透過該字串決定回覆的動作

伺服器接收到的 postback event 大致如下
特別注意到 postback.data 就是剛剛提到特定的字串
官方文檔

{
  "destination": "xxxxxxxxxx",
  "events": [
    {
      "replyToken": "b60d432864f44d079f6d8efe86cf404b",
      "type": "postback",
      "mode": "active",
      "source": {
        "userId": "U91eeaf62d...",
        "type": "user"
      },
      "timestamp": 1513669370317,
      "postback": {
        "data": "storeId=12345",
        "params": {
          "datetime": "2017-12-25T01:00"
        }
      }
    }
  ]
}

官方文檔
Postback Action 包含的參數大致如下
特別注意到

  • label 是顯示的內容
  • data 就是我們指定的字串
{
   "type":"postback",
   "label":"Buy",
   "data":"action=buy&itemid=111",
   "text":"Buy"
}

Message Action

官方文檔
Message Action 會讓使用者觸發後回覆特定的訊息內容

Message Action 包含的參數大致如下

  • label 是顯示的內容
  • text 就是我們指定的訊息內容
{
   "type":"message",
   "label":"Yes",
   "text":"Yes"
}

URI Action

官方文檔
URI Action 會讓使用者被轉向到指定的連結

URI Action包含的參數大致如下

  • label 是顯示的名稱
  • uri 就是我們指定的連結
  • altUri 是指定給MaxOS/Windows的用戶使用的連結
{
   "type":"uri",
   "label":"View details",
   "uri":"http://example.com/page/222",
   "altUri": {
      "desktop" : "http://example.com/pc/page/222"
   }
}

Datetime Picker Action

官方文檔
Datetime Picker Action 會觸發選擇時間的功能,讓使用者以 postback event 的方式,回傳所選擇的時間

Datetime Picker Action 包含的參數如下

  • label 是顯示的內容
  • data 是指定 postback event 回傳時一起帶過來的資料內容
  • mode 是選擇時間的模式,包含了
    • date
      只選擇日期
    • time
      只選擇時間
    • datetime
      選擇日期與時間
  • initial 是選擇時的預設起點
  • max 是選擇的範圍最大值
  • min 是選擇的範圍最小值
{
   "type":"datetimepicker",
   "label":"Select date",
   "data":"storeId=12345",
   "mode":"datetime",
   "initial":"2017-12-25t00:00",
   "max":"2018-01-24t23:59",
   "min":"2017-12-25t00:00"
}

補充:
上面所說的 initial max min 格式包含了 full-date time-hour time-minutes
分別代表 日期 小時 分鐘
在不同的mode下所需的格式不同

Mode Format Example
date full-date 2017-06-18
time time-hour:time-minutes 00:00
datetime full-dateTtime-hour:time-minutes 2017-06-18T06:15

Camera Action

官方文檔
Camera Action 會使 LINE 相機功能被觸發

Camera Action 包含的參數如下

  • label 是顯示的內容
{
   "type":"camera",
   "label":"Camera"
}

Camera Roll Action

官方文檔
Camera Roll Action 會使 LINE 觸發選取相片的功能

Camera Roll Action 包含的參數如下

  • label 是顯示的內容
{
   "type":"cameraRoll",
   "label":"Camera roll"
}

Location Action

官方文檔
Location Action 會使 LINE 觸發選取位置的功能

Location Action包含的參數如下

  • label 是顯示的內容
{
   "type":"location",
   "label":"Location"
}

後記

原本是打算直接講怎麼傳送那些蝦趴的訊息
但是每種訊息都可以搭配 Actions 使用,於是就先介紹各個 Actions 了
之前我都沒有特別注意到有哪些 Action,突然有種發現新大陸的感覺XD
原來之前口罩查訊系統根本不用做教學告訴使用者要怎麼自己傳位置資訊
直接給個 Location Action 就輕鬆解決問題了www

下一篇終於終於要進入到使用進階訊息傳送的功能了
希望各位也玩得開心囉~


上一篇
Day06 基本訊息傳送
下一篇
Day08 進階訊息傳送2 - Template Messages
系列文
LINE BOT 新手村30日攻略30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
arguskao
iT邦新手 4 級 ‧ 2022-12-21 17:15:11

怎麼沒有Github文檔?

這一篇完全看不懂

Koios iT邦新手 4 級 ‧ 2022-12-22 14:01:18 檢舉

當時想著 Template message 當中都時常會用上各種 Action,於是用一篇的篇幅說明 Action。不過並沒有好好說明 Action 可以做甚麼,如果光看這一篇的話確實會看不太懂,下一篇的 Template message 就會使用到 Action 囉!

現在回頭看兩年前寫的文章,確實有好多值得改進的地方,還請多多包涵><

arguskao iT邦新手 4 級 ‧ 2022-12-22 17:07:48 檢舉

你這一系列真是很棒,我本來看很多人寫的看不懂,你前面有寫得很清楚
後面我就又看不懂了....

我要留言

立即登入留言